package com.koron.order.common.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.dto.OrderDispatchDto;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.bean.po.OrderWorkDeptPo;
import com.koron.order.common.bean.vo.OrderDispatchVo;
import com.koron.order.common.service.OrderDispatchInfoService;
import com.koron.order.common.service.OrderDispatchService;
import com.koron.order.common.service.OrderDispatchWorkerService;
import com.koron.order.common.service.OrderWorkDeptService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName OrderDispatchInfoServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/10/14 15:05
 */
@Service
public class OrderDispatchInfoServiceImpl implements OrderDispatchInfoService {

    @Autowired
    OrderDispatchService orderDispatchService;

    @Autowired
    OrderDispatchWorkerService orderDispatchWorkerService;

    @Autowired
    OrderWorkDeptService orderWorkDeptService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public String insertOrUpdate(SessionFactory factory, OrderDispatchDto dto) {

        OrderDispatch orderDispatch = new OrderDispatch();

        BeanUtils.copyProperties(dto, orderDispatch);

        String orderDispatchId = orderDispatch.getId();
        String orderId = orderDispatch.getOrderId();

        if (StringUtils.isEmpty(orderDispatchId)) {
            orderDispatch.setId(CodeTools.getCode32());
        }

        orderDispatchService.insertOrUpdate(factory, orderDispatch);

        List<OrderDispatchWorker> orderDispatchWorker = dto.getOrderDispatchWorker();
        if (CollectionUtils.isNotEmpty(orderDispatchWorker)) {
            orderDispatchWorker.forEach(p -> {
                p.setDispatchId(orderDispatch.getId());
                p.setOrderId(orderId);
            });
        }
        //先删除再保存新增
        orderDispatchWorkerService.deleteByDispatchIdOrderId(factory, orderDispatch.getId(), orderId);
        orderDispatchWorkerService.insertBatch(factory, orderDispatchWorker);

        //vo拿到作业单位信息  入库  先根据工单id删除再插入数据
        List<OrderWorkDeptPo> orderWorkDeptPoList = dto.getOrderWorkDeptPoList();
        if (CollectionUtils.isNotEmpty(orderWorkDeptPoList)) {
            orderWorkDeptPoList.forEach(p -> {
                p.setOrderId(orderId);
                p.setDispatchId(orderDispatch.getId());
                p.setOrderType(orderDispatch.getOrderType());
            });
        }
        orderWorkDeptService.deleteByOrderId(factory, orderId);
        orderWorkDeptService.insertBatch(factory, orderWorkDeptPoList);

        return orderDispatch.getId();

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        orderDispatchService.deleteById(factory, id);
        orderDispatchWorkerService.deleteByDispatchId(factory, id);
        orderWorkDeptService.deleteByDispatchId(factory, id);
        return 1;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        orderDispatchService.deleteByOrderId(factory, orderId);
        orderDispatchWorkerService.deleteByOrderId(factory, orderId);
        orderWorkDeptService.deleteByOrderId(factory, orderId);
        return 1;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public OrderDispatchVo queryByOrderId(SessionFactory factory, String orderId) {
        OrderDispatchVo orderDispatchVo = new OrderDispatchVo();
        //分别查出来,set进整个对象
        OrderDispatch orderDispatch = orderDispatchService.queryByOrderId(factory, orderId);
        if (Objects.isNull(orderDispatch)) {
            return null;
        }
        BeanUtils.copyProperties(orderDispatch, orderDispatchVo);
        List<OrderDispatchWorker> orderDispatchWorkerList = orderDispatchWorkerService.queryByOrderId(factory, orderId);
        orderDispatchVo.setOrderDispatchWorker(orderDispatchWorkerList);

        //查询作业单位
        List<OrderWorkDeptPo> orderWorkDeptPos = orderWorkDeptService.queryByOrderId(factory, orderId);
        orderDispatchVo.setOrderWorkDeptPoList(orderWorkDeptPos);

        return orderDispatchVo;
    }

}


